package com.example.NIO;

import org.springframework.util.Assert;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

public class NioFileChannel04 {
    public static void main(String[] args) throws IOException {
        // 创建相关的流
        File infile = new File("a.txt");
        FileUtil.initFile(infile, FileUtil.pic);
        FileInputStream inputStream = new FileInputStream(infile);
        File outfile = FileUtil.newFile("b.txt");
        FileOutputStream outputStream = new FileOutputStream(outfile);

        FileChannel inputStreamChannel = inputStream.getChannel();
        FileChannel outputStreamChannel = outputStream.getChannel();

        // 使用 transfrom 完成拷贝
        outputStreamChannel.transferFrom(inputStreamChannel, 0, inputStreamChannel.size());

        Assert.isTrue(infile.length() == outfile.length(), "拷贝失败，两个文件大小不一样");

        inputStreamChannel.close();
        outputStreamChannel.close();
        inputStream.close();
        outputStream.close();
        infile.delete();
        outfile.delete();
    }
}
